package c;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class StringComparator implements Comparator<String> {

	@Override
	public int compare(String s1, String s2) {
		int len1 = s1.length();
		int len2 = s2.length();
		
		if (len1 > len2) {
			return 1;
		} else if (len1 < len2) {
			return -1;
		} else {
			return 0;
		}
				
	}
	
}

class Student {
	private int id;
	private String name;
	
	public Student(int id, String name) {
		this.id = id;
		this.name = name;
	}
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	public String toString() {
		return name;
	}
}

public class ComparatorTest {

	public static void main(String[] args) {
		//sorting String
        List<String> animals = new ArrayList<String>();
        
        animals.add("tiger");
        animals.add("lion");
        animals.add("cat");
        animals.add("fish");
        animals.add("elephant");
        animals.add("chicken");
                
        Collections.sort(animals, new StringComparator());
        
        for(String animal : animals) {
        	System.out.println(animal);
        }
        
        //Sorting Integer
        List<Integer> numbers = new ArrayList<Integer>();
        
        numbers.add(5);
        numbers.add(7);
        numbers.add(4);
        numbers.add(1);
        numbers.add(0);
        
        Collections.sort(numbers, new Comparator <Integer> () {
        	public int compare(Integer i1, Integer i2) {
        		return -i1.compareTo(i2);
        	}
        });
        
        System.out.println();
        
        for(Integer number : numbers) {
        	System.out.println(number);
        }
        
        //Sorting custom class
        
        List<Student> student = new ArrayList<Student>();
        
        student.add(new Student(1, "Jack"));
        student.add(new Student(2, "Belle"));
        student.add(new Student(5, "Micro"));
        student.add(new Student(3, "Bob"));
        
        Collections.sort(student, new Comparator<Student>() {
        	public int compare(Student s1, Student s2) {
        		return s1.getName().compareTo(s2.getName());
        	}
        });

        System.out.println();
        for(Student stu : student) {
        	System.out.println(stu.getId() + " : " + stu.getName());
        }
	}

}
